<%@include file="/html/book/init.jsp"%>


<h3>List of Books</h3>

<%

	List<BMSBook> booksTemp = (List<BMSBook>) request.getAttribute("SEARCH_RESULT");

	List<BMSBook> books = Validator.isNotNull(booksTemp) ?
			ListUtil.copy(booksTemp) :
				BMSBookLocalServiceUtil.getBMSBooks(0, -1);
	
	String orderByCol = (String) request.getAttribute("orderByCol");
	String orderByType = (String) request.getAttribute("orderByType");
	BeanComparator comp = new BeanComparator(orderByCol);
	
	booksTemp = ListUtil.copy(books);
	Collections.sort(booksTemp, comp);
	
	if (orderByType.equalsIgnoreCase("desc")){
		Collections.reverse(booksTemp);
	}
	books = ListUtil.copy(booksTemp);
	
	PortletURL iteratorURL = renderResponse.createRenderURL();
	iteratorURL.setParameter("jspPage", BookConstants.SHOW_BOOK);

    PortletURL deleteBookURL = renderResponse.createActionURL(); 
    deleteBookURL.setParameter(ActionRequest.ACTION_NAME, BookConstants.ACTION_DELETE_BOOK);
    deleteBookURL.setParameter("redirectURL",iteratorURL.toString());
    
    PortletURL bookDetailsURL = renderResponse.createRenderURL();
    bookDetailsURL.setParameter("jspPage", BookConstants.PAGE_DETAILS);
    bookDetailsURL.setParameter("backURL", themeDisplay.getURLCurrent());
    
%>
 <c:if test="<%= !books.isEmpty() %>">
		<% String functionName = renderResponse.getNamespace() + "submitFormForAction();"; %>
		<aui:button-row>
				<aui:button value="delete" cssClass="delete-books-button" onClick="<%= functionName %>"/>
		</aui:button-row>
</c:if>

<portlet:actionURL name="<%= BookConstants.ACTION_DELETE_BOOKS %>" var="deleteBooksURL">
		<portlet:param name="redirectURL" value="<%= iteratorURL.toString() %>"/>
</portlet:actionURL>

<aui:form action="<%= deleteBooksURL.toString() %>">
		<aui:input name="bookIdsForDelete" type="hidden"/>

		<liferay-ui:search-container delta="5" emptyResultsMessage="No Record Found"
										iteratorURL="<%= iteratorURL %>" orderByCol="<%= orderByCol %>" orderByType="<%= orderByType %>" 
										  rowChecker="<%= new RowChecker(renderResponse) %>" >
										
			<liferay-ui:search-container-results 
					total="<%= books.size() %>"
					results="<%= ListUtil.subList(books, 
							searchContainer.getStart(), searchContainer.getEnd()) %>" />
							
			<liferay-ui:search-container-row className="BMSBook" modelVar="book">
					<%
						bookDetailsURL.setParameter("bookId", Long.toString(book.getBookId()));
					%>
					<liferay-ui:search-container-column-text name="Book Title" property="bookTitle" href="<%= bookDetailsURL.toString() %>"
						orderable="true" orderableProperty="bookTitle" />
					<liferay-ui:search-container-column-text name="Author" property="author"
					 	orderable="true" orderableProperty="author" />
					<liferay-ui:search-container-column-text name="Date Added" orderable="true" orderableProperty="createDate" >
							<fmt:formatDate value="<%= book.getCreateDate() %>" pattern="dd/MMM/yyyy"/>
					</liferay-ui:search-container-column-text>
				<% deleteBookURL.setParameter("bookId", Long.toString(book.getBookId())); %>
					<liferay-ui:search-container-column-text name="Delete" 
							href="<%= deleteBookURL.toString() %>" value="delete" />
					<liferay-ui:search-container-column-jsp name="Actions" path="<%=BookConstants.PAGE_ACTIONS %>"/>		
			</liferay-ui:search-container-row>
			
			<liferay-ui:search-iterator searchContainer="<%= searchContainer %>"/>
			
		</liferay-ui:search-container>
</aui:form>

<br/><a href="<portlet:renderURL/>">&laquo; Go Back</a>


<aui:script use="aui-base">
		var deleteBooksBtn = A.one('.delete-books-button');
				
				if (deleteBooksBtn != 'undefined'){
					var toggleDisabled = function(disabled){
						deleteBooksBtn.one(':button').attr('disabled',disabled);
						deleteBooksBtn.toggleClass('aui-button-disabled',disabled);
					};
					
					var resultsGrid = A.one('.results-grid');
					
					if (resultsGrid) {
						resultsGrid.delegate(
						'click',
						function(event) {
							var disabled = (resultsGrid.one(':checked') == null);
							toggleDisabled(disabled);
						},
						':checkbox'
					);
				}
				toggleDisabled(true);
			}
			
		Liferay.provide(
				window,
				'<portlet:namespace/>submitFormForAction',
				function() {
					var accepted = confirm('<%= 
						UnicodeLanguageUtil.get(pageContext,
						"are-you-sure-you-want-to-delete-selected-books") %>');
					
					if (accepted) {
						var frm = document.<portlet:namespace/>fm;
						var hiddenField = frm.<portlet:namespace/>bookIdsForDelete;
						
						hiddenField.value = 
								Liferay.Util.listCheckedExcept (
									frm, "<portlet:namespace/>allRowIds");
								submitForm(frm);
					}
				},
				['liferay-util-list-fields']
		);
</aui:script>
